package com.zzu.chat.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zzu.chat.R;
import com.zzu.chat.bean.Blink;
import com.zzu.chat.chat.BlinkAdapter;
import com.zzu.chat.chat.UploadActivity;
import com.zzu.chat.util.TimeUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.leancloud.AVObject;
import cn.leancloud.AVQuery;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class DynamicActivity extends AppCompatActivity {


    private FloatingActionButton floatingActionButton;
    private RecyclerView recyclerView;
    private List<Blink> blinkList = new ArrayList<>();
    private SmartRefreshLayout refreshLayout;
    private BlinkAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic);
        floatingActionButton = findViewById(R.id.fab);
        recyclerView = findViewById(R.id.recyclerView);
        refreshLayout = findViewById(R.id.refreshLayout);

        //打开上传页面
        floatingActionButton.setOnClickListener(view -> {
            Intent intent = new Intent(DynamicActivity.this, UploadActivity.class);
            startActivity(intent);
        });

        //列表
        recyclerView.setHasFixedSize(true);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
//        queryBlink();
//        adapter = new BlinkAdapter(blinkList);
//        recyclerView.setAdapter(adapter);
        MyThread thread= new MyThread();
        thread.start();


        //刷新
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                queryBlink();
//                adapter = new BlinkAdapter(blinkList);
                MyThread thread= new MyThread();
                thread.start();
                recyclerView.setAdapter(adapter);
                refreshLayout.finishRefresh();
            }
        });
        refreshLayout.autoRefresh();

        // 更换标题
        ActionBar actionBar = getSupportActionBar();
        if (actionBar!=null){
            actionBar.hide();
        }
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what== 0x11){
                blinkList = (List<Blink>) msg.getData().getSerializable("blinkList");
                System.out.println("handlerBlink:"+blinkList.size());
                adapter = new BlinkAdapter(blinkList);
                recyclerView.setAdapter(adapter);
            }
        }
    };
    // 启动该活动
    public static void actionStart(Context context) {
        Intent intent = new Intent(context, DynamicActivity.class);
        context.startActivity(intent);
    }


    /**
     * 查找
     */
    private void queryBlink() {
        blinkList.clear();
        new Thread(new Runnable() {
            @Override
            public void run() {
                AVQuery<AVObject> query = new AVQuery<>("Blink");
                // 获取最新发布的
                query.orderByDescending("createdAt");
                query.include("blinkImageURLList");
                // 只获取 10 条
                query.limit(10);

                query.findInBackground().subscribe(new Observer<List<AVObject>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<AVObject> avObjects) {
                        for (AVObject avObject:avObjects){
                            blinkList.add(TimeUtils.getBlinkClass(avObject));
                        }


                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
            }
        }).start();
    }

    class MyThread extends Thread{
        @Override
        public void run() {
            super.run();
            AVQuery<AVObject> query = new AVQuery<>("Blink");
            // 获取最新发布的
            query.orderByDescending("createdAt");
            query.include("blinkImageURLList");
            // 只获取 10 条
            query.limit(10);

            query.findInBackground().subscribe(new Observer<List<AVObject>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(List<AVObject> avObjects) {

                    List<Blink> blinkList5 = new ArrayList<>();
                    for (AVObject avObject:avObjects){
                        blinkList5.add(TimeUtils.getBlinkClass(avObject));
                    }

                    // 通过Handler传递到UI线程中
                    Message message = new Message();
                    message.what = 0x11;
                    Bundle bundle = new Bundle();

                    bundle.putSerializable("blinkList", (Serializable) blinkList5);

                    message.setData(bundle);
                    handler.sendMessage(message);

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });

        }
    }


}